<template>
  <div class="box">
    <el-card>
      <template #header>极客园</template>

      <el-form
        lable-width="5px"
        ref="loginRef"
        :rules="rules"
        :model="loginForm"
      >
        <el-form-item label="用户名" prop="mobile">
          <el-input
            placeholder="请输入电话号码"
            v-model.trim="loginForm.mobile"
            prop="mobile"
            ref=""
          >
          </el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input
            placeholder="请输入验证码"
            v-model.trim="loginForm.code"
            type="text"
            @keyup.enter.native="login"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="checked"
            >我已阅读并同意「用户协议」和「隐私条款」</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// import { login } from '@/api/user'
export default {
  name: 'LoginPage',
  data () {
    return {
      checked: false,
      loginForm: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { min: 11, max: 11, message: '长度是11位电话号码', trigger: 'blur' },
          {
            pattern: /^[0-9]*$/,
            message: '用户名只能是数字字母下划线',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 6, max: 6, message: '必须是6位验证码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async login () {
      console.log(this.loginForm)
      if (this.checked) {
        // console.log(this.loginForm)
        try {
          // 二次效验
          await this.$refs.loginRef.validate()
          await this.$store.dispatch('user/login', this.loginForm)
          this.$message.success('登录成功')
          this.$router.push('/home')
        } catch (e) {
          console.log(e)
        }
      } else {
        this.$message('你必须同意使用条款')
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.box {
  width: 100vw;
  height: 100vh;
  //   background: url("@/assets/login-bg.svg") no-repeat center;
  background-color: #66ccff;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-card {
  width: 420px;
  height: 390px;
}

::v-deep .el-card__header {
  background-color: #727cf5;
  text-align: center;
  color: #fff;
}
</style>
